package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.IndustryService;
import com.junzhiit.nengji.cms.web.exception.IndustryExistShopException;
import com.junzhiit.nengji.cms.web.exception.IndustryNotFoundException;
import com.junzhiit.nengji.cms.web.exception.NameIsExistException;
import com.junzhiit.nengji.cms.web.util.Constants;
import com.junzhiit.nengji.cms.web.util.OrderNumberUtils;
import com.junzhiit.nengji.cms.web.vo.IndustryVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.IndustryDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.model.Industry;
import com.junzhiit.nengji.model.Shop;

@Service
public class IndustryServiceImpl extends BaseServiceImpl implements IndustryService {

	@Autowired
	private IndustryDAO industryDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public ResultVO getIndustryList(Industry industry) {
		ResultVO result = new ResultVO();
		List<Industry> typeList = industryDAO.findAll(null);
		List<IndustryVO> list = treeList(typeList, Constants.ROOT_VALUE);
		result.setData(list);
		return result;
	}

	public List<IndustryVO> treeList(List<Industry> typeList, String parentId) {  
		List<IndustryVO> childTypeList = new ArrayList<IndustryVO>();
		for (Industry industry : typeList) {
			IndustryVO industryVO = new IndustryVO();
			BeanUtils.copyProperties(industry, industryVO);
			String id = industry.getId();
			String pid = industry.getParentId();
			if (parentId.equals(pid)) {
				List<IndustryVO> childList = treeList(typeList, id);
				industryVO.setChildren(childList);
				industryVO.setChildCount(childList.size());
				childTypeList.add(industryVO);
			}
		}
		return childTypeList;  
	}

	@Override
	public ResultVO getIndustryFullName(String parentId) {
		ResultVO result = new ResultVO();
		result.setData(industryDAO.getIndustryFullName(parentId));
		return result;
	}

	@Override
	public ResultVO add(Industry industry) {
		ResultVO result = new ResultVO();
		int count = industryDAO.checkName(industry);
		if(count>0){
			//已存在该名称
			throw new NameIsExistException("已存在该名称");
		}else{
			String maxCode = null;
			industry.setFullName(industry.getName());
			if(StringUtils.isNotBlank(industry.getParentId()) && !Constants.ROOT_VALUE.equals(industry.getParentId())){//添加子类
				//获得父类型的信息
				Industry industryParent = industryDAO.query(industry.getParentId());
				//获得该子类的最大Code
				String childMaxCode = industryDAO.getMaxCode(industryParent.getId());
				
				childMaxCode = getTypeCode("",childMaxCode);
				maxCode = industryParent.getCode()+childMaxCode;
				
				industry.setFullName(industryParent.getFullName()+Constants.TYPE_FULLNAME_SEPARATOR+ industry.getName());
				industry.setLevel(industryParent.getLevel()+1);
				industry.setParentId(industryParent.getId());
				int maxSort =  industryDAO.getMaxSort(industryParent.getId());
				industry.setSort(maxSort+Constants.TYPE_SORT_ONE);
			}else{//添加頂級root
				industry.setSort(industryDAO.getMaxSort(Constants.ROOT_VALUE)+Constants.TYPE_SORT_ONE);
				maxCode = industryDAO.getMaxCode(Constants.ROOT_VALUE);
				maxCode = getTypeCode(OrderNumberUtils.TYPE_NUMBER_PREFIX,maxCode);
				industry.setLevel(1);
				industry.setSort(1);
				industry.setParentId(Constants.ROOT_VALUE);
			}
			industry.setCode(maxCode);
			industryDAO.insert(industry);
		}
		return result;
	}
	
	
	//获得类型编号Code
	private String getTypeCode(String prefix, String maxCode){
		int codeValue = 1;
		if(StringUtils.isNotBlank(maxCode)){
			int currentMaxCode = Integer.valueOf(maxCode.substring(maxCode.length()-3, maxCode.length()));
			codeValue += currentMaxCode;
		}
		String code = OrderNumberUtils.getSystemCode(prefix, codeValue);
		return code;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		Industry query = industryDAO.query(id);
		if(query == null){
			throw new IndustryNotFoundException("找不到行业类型");
		}
		List<Shop> shops = shopDAO.getShopByIndustry(id);
		if(shops != null && !shops.isEmpty()){
			throw new IndustryExistShopException("该行业下仍存在商家，请删除后再删除该行业！");
		}
		
		industryDAO.updateToDelete(id);
		if(query.getParentId().equals(Constants.ROOT_VALUE)){
			List<Industry> list = industryDAO.findAll(query.getId());
			for(Industry industry : list){
				industryDAO.updateToDelete(industry.getId());
			}
		}
		return result;
	}

	@Override
	public ResultVO getIndustryById(String id) {
		ResultVO result = new ResultVO();
		Industry industry = industryDAO.getIndustryById(id);
		result.setData(industry);
		return result;
	}

	@Override
	public ResultVO update(Industry industry) {
		ResultVO result = new ResultVO();
		int count = industryDAO.checkName(industry);
		if(count>0){
			//已存在该名称
			throw new NameIsExistException("已存在该名称");
		}
		Industry query = industryDAO.query(industry.getId());
		if(industry.getParentId().equals(Constants.ROOT_VALUE)){
			query.setName(industry.getName());
			query.setFullName(industry.getName());
			industryDAO.update(query);
			List<Industry> list = industryDAO.getIndustryByParentId(industry.getId());
			if(list.size() > 0){
				for(Industry params : list){
					params.setFullName(industry.getName()+Constants.TYPE_FULLNAME_SEPARATOR+ params.getName());
					industryDAO.update(params);
				}
			}
		}else{
			Industry params = industryDAO.query(industry.getParentId());
			query.setFullName(params.getName()+Constants.TYPE_FULLNAME_SEPARATOR+ industry.getName());
			query.setName(industry.getName());
			industryDAO.update(query);
		}
		return result;
	}

	@Override
	public ResultVO getAll() {
		ResultVO result=new ResultVO();
		List<Industry> list=industryDAO.findAll(null);
		result.setData(list);
		return result;
	}

}
